﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Register.BLL;
using System.Web.Script.Serialization;
using Maticsoft.Model.Register;
using Register.Model;
using System.Text;
namespace RegisterWeb.Manager.RegistrationFeeManager.ashx
{
    /// <summary>
    /// PayForRegistrantionService 的摘要说明
    /// </summary>
    public class PayForRegistrantionService : IHttpHandler
    {
       static     List<PayForRegistrantionJSON> listjson = new List<PayForRegistrantionJSON>();
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string action = context.Request["action"];      
             
            if (action == "get")
            {
                listjson.Clear();
          //   List<PayForRegistrantionJSON> listjson = new List<PayForRegistrantionJSON>();
                int flag = 1;
                StringBuilder strBuilder = new StringBuilder();  
                int intPageSize = int.Parse(context.Request["rows"].ToString());
                int intCurrentPage = int.Parse(context.Request["page"].ToString());
              //  var list = PayForRegistrantionInfoBLL.GetAllPayForRegistrantion("1=1", intCurrentPage, intPageSize);
                string Departments_ID = (string)context.Request["departments"];
                string OutpatientType = (string)context.Request["rfoutpatientype"];    
              string dt1 =(string)context.Request["date1"];
              string dt2 = (string)context.Request["date2"];
        
                if (!String.IsNullOrEmpty(Departments_ID))
                {
                    strBuilder.Append("   Departments_ID='").Append(Departments_ID).Append("' ");
                    flag = 0;
                }
              
                if (!String.IsNullOrEmpty(OutpatientType))
                {
                    if (flag == 1)
                    {
                        strBuilder.Append(" Outpatient_OutpaitentType='").Append(OutpatientType).Append("' ");
                    }
                    else
                        strBuilder.Append(" and  Outpatient_OutpaitentType='").Append(OutpatientType).Append("' ");
                    flag = 0;
                }
                if (!String.IsNullOrEmpty(dt1))
                {

                    DateTime date1=Convert.ToDateTime(dt1);
                    if (flag == 1)
                    {
                        strBuilder.Append(" PayForRegistrantion_AddTime>'").Append(date1).Append("' ");
                    }
                    else
                        strBuilder.Append(" and PayForRegistrantion_AddTime>'").Append(date1).Append("' ");
                    flag = 0;
                }

                if (!String.IsNullOrEmpty(dt2))
                {

                    DateTime date2 = Convert.ToDateTime(dt2);
                    if (flag == 1)
                    {
                        strBuilder.Append(" PayForRegistrantion_AddTime<'").Append(date2).Append("' ");
                    }
                    else
                        strBuilder.Append(" and PayForRegistrantion_AddTime<'").Append(date2).Append("' ");
                    flag = 0;
                }

                if (flag == 1)
                {
                    strBuilder.Append("1=1");
                }
                var list = PayForRegistrantionviewBLL.GetList(strBuilder.ToString(), intCurrentPage, intPageSize);
             
                foreach (var row in list)
                {
                    PayForRegistrantionJSON json1 = new PayForRegistrantionJSON(row.PayForRegistrantion_AddTime.ToString(),row.Appointment_ID,row.Departments_ID,row.Departments_Name,row.Doctor_ID,row.Hospital_ID,row.Hospital_Name,row.Outpatient_ID,row.Outpatient_OutpaitentType,row.PayForRegistrantion_ID,row.PayForRegistrantion_Menory,row.PayForRegistrantion_Recoder,row.PayForRegistrantion_Type,row.Register_ID);
                    listjson.Add(json1);
                }
                PayForRegistrantionjson json = new PayForRegistrantionjson();
                json.rows = listjson;
                json.total = PayForRegistrantionviewBLL.GetListCount(strBuilder.ToString());
                JavaScriptSerializer jss = new JavaScriptSerializer();
                string ss=  jss.Serialize(json);
                context.Response.Write(ss);
                action = action;
            }
            if (action == "getExcel")
            {
                //int flag = 1;
                ////int intPageSize = int.Parse(context.Request["rows"].ToString());
                ////int intCurrentPage = int.Parse(context.Request["page"].ToString());
                HttpContext.Current.Response.ContentType = "application/ms-excel";
                //string Hospital_ID = (string)context.Request["hpname"];
                //string OutpatientType = (string)context.Request["rfoutpatientype"];
                //string dt1 = (string)context.Request["date1"];
                //string dt2 = (string)context.Request["date2"];
                //StringBuilder strBuilder = new StringBuilder();
                //if (!String.IsNullOrEmpty(Hospital_ID))
                //{
                //    strBuilder.Append(" Hospital_ID='").Append(Hospital_ID).Append("' ");
                //    flag = 0;
                //}
                //if (!String.IsNullOrEmpty(OutpatientType))
                //{
                //    if (flag == 1)
                //    {
                //        strBuilder.Append(" Outpatient_OutpaitentType='").Append(OutpatientType).Append("' ");
                //    }
                //    else
                //        strBuilder.Append(" and  Outpatient_OutpaitentType='").Append(OutpatientType).Append("' ");
                //    flag = 0;
                //}
                //if (!String.IsNullOrEmpty(dt1))
                //{

                //    DateTime date1 = Convert.ToDateTime(dt1);
                //    if (flag == 1)
                //    {
                //        strBuilder.Append(" PayForRegistrantion_AddTime>'").Append(date1).Append("' ");
                //    }
                //    else
                //        strBuilder.Append(" and PayForRegistrantion_AddTime>'").Append(date1).Append("' ");
                //    flag = 0;
                //}
                //if (!String.IsNullOrEmpty(dt2))
                //{
                //    DateTime date2 = Convert.ToDateTime(dt2);
                //    if (flag == 1)
                //    {
                //        strBuilder.Append(" PayForRegistrantion_AddTime<'").Append(date2).Append("' ");
                //    }
                //    else
                //        strBuilder.Append(" and PayForRegistrantion_AddTime<'").Append(date2).Append("' ");
                //    flag = 0;
                //}

                //if (flag == 1)
                //{
                //    strBuilder.Append("1=1");
                //}
               
                //var list = PayForRegistrantionviewBLL.GetList(strBuilder.ToString(), 1,  PayForRegistrantionInfoBLL.GetPayForRegistrantionCount(strBuilder.ToString()));
                //List<PayForRegistrantionJSON> listjson = new List<PayForRegistrantionJSON>();
                //foreach (var row in list)
                //{
                //    PayForRegistrantionJSON json1 = new PayForRegistrantionJSON(row.PayForRegistrantion_AddTime.ToString(),row.Appointment_ID,row.Departments_ID,row.Departments_Name,row.Doctor_ID,row.Hospital_ID,row.Hospital_Name,row.Outpatient_ID,row.Outpatient_OutpaitentType,row.PayForRegistrantion_ID,row.PayForRegistrantion_Menory,row.PayForRegistrantion_Recoder,row.PayForRegistrantion_Type,row.Register_ID);
                //    listjson.Add(json1);
                //}  
                HttpResponse resp;
                resp = context.Response;// Page.Response;
                resp.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
                resp.AppendHeader("Content-Disposition", "attachment;filename=" + "Excel.xls");
                string colHeaders = "", ls_item = "";
                    colHeaders += "编号" + "\t";
                    colHeaders += "医院名" + "\t";
                    colHeaders += "科室名" + "\t";
                    colHeaders += "门诊类型" + "\t";
                    colHeaders += "支付方式" + "\t";
                    colHeaders += "添加时间" + "\t";
                    colHeaders += "费用" + "\n";
                    resp.Write(colHeaders);
                     //向HTTP输出流中写入取 得的数据信息 
                    //逐行处理数据   
                    for (int i = 0; i < listjson.Count; i++)
                {
                    ls_item=ls_item+listjson[i].payForRegistrantion_ID+"\t";
                    ls_item=ls_item+listjson[i].hospital_Name+"\t";
                    ls_item=ls_item+listjson[i].departments_Name+"\t";
                    ls_item=ls_item+listjson[i].outpatient_OutpaitentTypeName+"\t";
                    ls_item=ls_item+listjson[i].payForRegistrantion_Type+"\t";
                   ls_item=ls_item+listjson[i].payForRegistrantion_AddTime+"\t";
                   ls_item=ls_item+listjson[i].payForRegistrantion_Menory+"\n";
                   resp.Write(ls_item);
                    ls_item = "";
                }
                resp.End();  
              //  json.total = PayForRegistrantionInfoBLL.GetPayForRegistrantionCount("1=1");
              // JavaScriptSerializer jss = new JavaScriptSerializer()
              //string ss = jss.Serialize(json);     
              //context.Response.Write(ss);
          
            }
        }
        class PayForRegistrantionjson
        {
            public int total { get; set; }
            public List<PayForRegistrantionJSON> rows { get; set; }

        }
        class PayForRegistrantionJSON
        {
            public String payForRegistrantion_ID { get; set; }
            public String register_ID { get; set; }
            public String payForRegistrantion_Type { get; set; }
            public Decimal payForRegistrantion_Menory { get; set; }
            public String payForRegistrantion_AddTime { get; set; }
            public String payForRegistrantion_Recoder { get; set; }
            public String outpatient_ID { get; set; }
            public String outpatient_OutpaitentType { get; set; }
            public String appointment_ID { get; set; }
            public String departments_ID { get; set; }
            public String doctor_ID { get; set; }
            public String hospital_ID { get; set; }
            public String departments_Name { get; set; }
            public String hospital_Name { get; set; }
            public String outpatient_OutpaitentTypeName{get; set; }

            public PayForRegistrantionJSON(String payForRegistrantion_AddTime, String appointment_ID, String departments_ID, String departments_Name, String doctor_ID, String hospital_ID, String hospital_Name, String outpatient_ID, String outpatient_OutpaitentType, String payForRegistrantion_ID, Decimal payForRegistrantion_Menory, String payForRegistrantion_Recoder, String payForRegistrantion_Type, String register_ID)
            {
                this.payForRegistrantion_AddTime = payForRegistrantion_AddTime;
                this.appointment_ID = appointment_ID;
                this.departments_ID = departments_ID;
                this.departments_Name = departments_Name;
                this.doctor_ID = doctor_ID;
                this.hospital_ID = hospital_ID;
                this.hospital_Name = hospital_Name;
                this.outpatient_ID = outpatient_ID;
                this.outpatient_OutpaitentType = outpatient_OutpaitentType;
                this.payForRegistrantion_ID = payForRegistrantion_ID;
                this.payForRegistrantion_Menory = payForRegistrantion_Menory;
                this.payForRegistrantion_Recoder=payForRegistrantion_Recoder;
                 this.payForRegistrantion_Type = payForRegistrantion_Type;
                this.register_ID=register_ID;
                this.outpatient_OutpaitentTypeName=ParamterHelper.GetParameterNameByTypeAndID("regType",outpatient_OutpaitentType);
            }
        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}